package org.example.server.sever.serviceImpl;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.pojo.envcontent.envContentCarouselData;
import org.example.pojo.envcontent.envContentCarouselDto;
import org.example.pojo.user.Emp;
import org.example.server.mapper.envContentCarouselDataMapper;
import org.example.server.sever.envContentService;
import org.example.server.sever.userService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class envContentServiceImpl extends ServiceImpl<envContentCarouselDataMapper, envContentCarouselData> implements envContentService {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Override
    public envContentCarouselDto getCarouselData(Integer pageNo , Integer pageSize) {
        //List<envContentCarouselData> carouselData = baseMapper.selectList(null);
        envContentCarouselDto carouselDto = new envContentCarouselDto();

        // 从缓存中获取数据
        String carouselDataStr = stringRedisTemplate.opsForValue().get("carouselData" + pageNo);
        if (carouselDataStr != null) {
            // 1. 先将JSON字符串转为DTO（此时data可能是List<JSONObject>类型）
            envContentCarouselDto bean = JSONUtil.toBean(carouselDataStr, envContentCarouselDto.class);
            carouselDto.setTotal(bean.getTotal());

            // 2. 直接从原始JSON字符串中解析data数组（关键修复）
            JSONArray dataArray = JSONUtil.parseObj(carouselDataStr).getJSONArray("data");
            if (dataArray != null) {
                // 转换为指定泛型的List
                List<envContentCarouselData> dataList = JSONUtil.toList(dataArray, envContentCarouselData.class);
                carouselDto.setData(dataList);
            }
            return carouselDto;
        }


        Page<envContentCarouselData> objectPage = Page.of(pageNo, pageSize);
        // 按id升序排序
        objectPage.addOrder(OrderItem.desc("id"));


        //分页查询
        Page<envContentCarouselData> page = page(objectPage);

        //总条数
        long total = page.getTotal();
        System.out.println(total);
        //总页数
        long totalPages = page.getPages();
        System.out.println(totalPages);
        // 分页数据
        List<envContentCarouselData> records = page.getRecords();
        records.forEach(System.out::println);
        carouselDto.setTotal((int) total);
        carouselDto.setData(records);
        //缓存数据
        stringRedisTemplate.opsForValue().set("carouselData"+pageNo, JSONUtil.toJsonStr(carouselDto),10, TimeUnit.MINUTES);

        return carouselDto;
    }


}
